package com.rover.service.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grid.Page;
import com.rover.dao.BaseDao;
import com.rover.entity.system.Department;

@Service("departmentService")
@Transactional
public class DepartmentService{

	@Autowired
	private BaseDao<Department> departmentDao;
	
	@Transactional(readOnly = true)
    public Page<Department> query(Page<Department> page,Map<String, Object> params, String hql){
		List<Department> departments = new ArrayList<Department>();
		if(StringUtils.isNotBlank(hql)){
			hql = "from Department " + hql;
			page.setTotalCount(departmentDao.count("select count(*) " + hql,params));
			departments = departmentDao.find(hql,params, page.getPageNo(),page.getPageSize());
		}else {
			hql = "from Department";
			page.setTotalCount(departmentDao.count("select count(*) " + hql));
			departments = departmentDao.find(hql, page.getPageNo(),page.getPageSize());
		}
		 
		for(Department department : departments){
			department.setpDepartment(null);
		}
		page.setResult(departments);
	    return page;
	}
	
	@Transactional(readOnly = true)
    public List<Department> getList(){
		return	departmentDao.find("from Department dept where dept.pDepartment is null");
	}
	
	@Transactional
    public void save(Department department){
		if(department.getId()==null){
			department.setDeptCode(getNextCode(department.getpDeptCode()));
			Department pDepartment=null;
			List<Department> li=departmentDao.find("from Department where deptCode=?",department.getpDeptCode());
			if(li.size()>0){
				pDepartment=li.get(0);
			}
			department.setpDepartment(pDepartment);
		}
		departmentDao.saveOrUpdate(department);
	}
	
	@Transactional
    public void delete(Long id){
		departmentDao.delete(Department.class, id);
	}
    /// <summary>
    /// 取得下一个部门的编号
    /// </summary>
    /// <param name="con"></param>
    /// <param name="ParentDeptCode"></param>
    /// <returns></returns>
    private String getNextCode(String ParentDeptCode)
    {
        String SQL = "select max(substring(deptCode,(length(deptCode)-3+1),3)) from Department where 1=1  ";

        if (ParentDeptCode == "")
        {
            SQL += " and length(deptCode)=3 ";
        }
        else
        {
            SQL += " and length(deptCode)=" + (ParentDeptCode.length() + 3) + " and substring(deptCode,1," + ParentDeptCode.length()+ ")='" + ParentDeptCode + "' ";
        }
        Object MaxID = departmentDao.getCurrentSession().createQuery(SQL).uniqueResult();
        
        if (MaxID == null)
        {
            if (ParentDeptCode == "")
                return "001";
            else
                return ParentDeptCode + "001";
        }
        else
        {
            if (ParentDeptCode == "")
                return String.format("%03d", (Long.valueOf(MaxID.toString()) + 1l));
            else
                return ParentDeptCode + String.format("%03d", (Long.valueOf(MaxID.toString()) + 1l));
        }

    }
}
